/**
 * 服务预约维护 - 编辑页面
 * @author miaoyl
 * @date 2019/04/04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail:false,
            form: {
                GWAK001: null,
                GWAKL003: null,
                MMRD013S: null,
                mmrd015_:null,
                operation: null
            },
            count:0,
            templateImageUrl:[],
            dialogImageUrl: '',
            dialogVisible: false,
            img_list:[],
            imgs: g_ctxPath + 'upload/imgs',
            rules: {
                MMRD001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRD002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRD019: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD020: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                mmrd001Options:app.datas_("crm_gwai740_mmrd001"),
                mmrd005Options:app.datas_("crm_gwai740_mmrd005"),
                mmrd019Options:app.datas_("crm_gwai740_mmrd019"),
                mmrd020Options:app.datas_("crm_gwai740_mmrd020"),
                mmrd013Options:[]
            },
        },
        callObj,
        vue
    ;

    var Gwai740Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai740_edit',
                _loadTplsFull: [/*{
                    dom: 'common_open_kh', tpl: 'common/_open_kh'
                },*/{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
                ]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwai740/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwai740').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwai740/add',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai740').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            vue.$refs['form'].resetFields();
            vueData.templateImageUrl = [];
            vueData.dialogImageUrl = '';
            vueData.dialogVisible = false;
            vueData.img_list = [];
            vueData.count = 0;
            vueData.form.operation = null;
            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (tagData.is_add) {
                vueData.isAdd=true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        MMRD019:'1',
                        MMRD020:'1'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                callObj.data.MMRD002_NAME = callObj.data.MMRA004 + '' + callObj.data.MMRA006;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(callObj.MMRD013 != undefined && callObj.MMRD013 != ''){
                    vueData.form.MMRD013S = callObj.data.MMRD013.split(',');
                }else {
                    vueData.form.MMRD013S = [];
                }
                vueData.form.mmrd015_ = callObj.data.MMRD015;
                if(callObj.data.MMRD015 != undefined && callObj.data.MMRD015.length > 0){
                    var list_mmrs015 = callObj.data.MMRD015.split(',');
                    list_mmrs015.forEach(function (v){
                        vueData.templateImageUrl.push({
                            name:v,url:'/wissun-smfile/' + v
                        });
                        vueData.img_list.push({
                            MMRD015: v
                        })
                    });
                }
                self.getMmrd013(callObj.data.MMRD002);
            }
        },

        getMmrd013:function (MMRD002) {
            app.ajax({
                xurl: 'crm/gwai740/getMmrd013List',
                xdata: {MMRD002: MMRD002},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.mmrd013Options = result.data.data;
                    }
                }
            });
        },

        _ready: function () {
            vueData.form = app.fields_(['MMRD_T'],vueData.form);
            vue = new Vue({
                el: '#gwai740_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                vueData.form.MMRD013 = vueData.form.MMRD013S.join(",");
                                vueData.form.MMRD015 = '';
                                vueData.form.mmrd015_ = '';
                                for (var i = 0; i < vueData.img_list.length; i++) {
                                    if (i == 0) {
                                        vueData.form.mmrd015_ = vueData.img_list[i].MMRD015;
                                    } else {
                                        vueData.form.mmrd015_ = vueData.form.mmrd015_ + ',' + vueData.img_list[i].MMRD015;
                                    }
                                }
                                vueData.img_list.forEach(function (v) {
                                    vue.$refs.upload.uploadFiles.removeByMap({
                                        name: v.MMRD015
                                    });
                                });
                                var flag = true;
                                if (vue.$refs.upload.uploadFiles.length > 0) {
                                    flag = false;
                                    vue.$refs.upload.submit();
                                }
                                if(flag){
                                    vueData.img_list.forEach(function (v) {
                                        vueData.form.MMRD015 = vueData.form.MMRD015 + ',' + v.MMRD015;
                                    });
                                    if(vueData.form.MMRD015 != '' && vueData.form.MMRD015 != null){
                                        vueData.form.MMRD015 = vueData.form.MMRD015.substring(1,vueData.form.MMRD015.length);
                                    }
                                    if (vueData.isAdd) {
                                        self.addData();
                                    } else {
                                        vueData.form.operation = 'edit';
                                        self.editData();
                                    }
                                }
                            }
                        });
                    },
                    handleRemove :function(file) {
                        if (!vueData.isAdd) {
                            vueData.img_list.removeByMap({
                                MMRD015: file.name
                            });
                            file = '';
                        }
                    },
                    handlePreview:function(file) {
                        vueData.dialogImageUrl = file.url;
                        vueData.dialogVisible = true;
                    },
                    beforeUpload :function(file) {
                        const isJPG = file.type === 'image/jpeg';
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        var imgFile = new FileReader();
                        if(isJPG && isLt2M){
                            imgFile.readAsDataURL(file);
                        }
                        if (!isJPG) {
                            this.$message.error("上传图片只能是 JPG 格式!");//上传图片只能是 JPG 格式!
                        }
                        if (!isLt2M) {
                            this.$message.error("上传图片大小不能超过 2MB!");//上传图片大小不能超过 2MB!
                        }
                        return isJPG && isLt2M;
                    },
                    successUpload :function(response) {
                        if (response.code == 200) {
                            vue.$message.success('图片上传成功');
                            if (vueData.form.mmrd015_ == '' || vueData.form.mmrd015_ == null) {
                                vueData.form.mmrd015_ = response.data[0].name;
                            } else {
                                vueData.form.mmrd015_ = vueData.form.mmrd015_ + ',' + response.data[0].name;
                            }
                            vueData.count++;
                            if (vueData.count == vue.$refs.upload.uploadFiles.length) {
                                vueData.img_list = [];
                                var data2 = vueData.form.mmrd015_.split(',');
                                for (var i = 0; i < data2.length; i++) {
                                    vueData.img_list.push({
                                        MMRD015: data2[i]
                                    })
                                }
                                var mmrd015_str = '';
                                vueData.img_list.forEach(function (v) {
                                    mmrd015_str = mmrd015_str + ',' + v.MMRD015;
                                });
                                if(mmrd015_str != '' && mmrd015_str != null){
                                    vueData.form.MMRD015 = mmrd015_str.substring(1,mmrd015_str.length);
                                }
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    vueData.form.operation = 'edit';
                                    self.editData();
                                }
                                vue.$refs.upload.clearFiles();
                            }
                        }else {
                            vue.$message.error('图片上传失败');
                        }
                    },
                    openGwak:function () {//客户开窗
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAK001 = data.GWAK001;
                                    vueData.form.GWAKL003 = data.GWAKL003;
                                    vueData.form.MMRD010 = data.GWAK031;
                                    vueData.form.MMRD011 = data.GWAK030;
                                    vueData.form.MMRD012 = data.GWAK028;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户信息',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    clearGwak001:function() {
                        vueData.form.GWAK001 = '';
                        vueData.form.GWAKL003 = '';
                        vueData.form.MMRD010 = '';
                        vueData.form.MMRD011 = '';
                    },
                    openMmrd002:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    columnList: [
                                        {label: '设备编号', prop: 'MMRA002', width: '150'},
                                        {label: '设备名称', prop: 'MMRA004', width: '150'},
                                        {label: '规格', prop: 'MMRA006', width: '150'},
                                        {label: '序列号', prop: 'MMRA003', width: '150'}
                                    ],
                                    url: 'crm/gwai740/getMmraList',
                                    searchParam:{
                                        MMRA001: vueData.form.GWAK001
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.MMRD002 = data.MMRA002;
                                    vueData.form.MMRD002_NAME = data.MMRA004 + '' + data.MMRA006;
                                    vueData.form.MMRD003 = data.MMRA003;
                                    vueData.form.MMRD013S = [];
                                    self.getMmrd013(data.MMRA002);
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品/设备信息',
                                area: ['800px', '470px']
                            }
                        });
                    },
                    clearMmrd002:function() {
                        vueData.form.MMRD002_NAME = '';
                        vueData.form.MMRD003 = '';
                        vueData.form.MMRD013S = [];
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai740_edit');
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwai740Edit();

});